﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Controls
{
    /// <summary>
    /// SimplePageControl.xaml 的交互逻辑
    /// </summary>
    public partial class SimplePageControl : UserControl, INotifyPropertyChanged
    {
        public event EventHandler<PageChangedEventArgs> evtPageChanged;
        private string _pageUpText = @"<<";
        /// <summary>
        /// 前翻页文字内容,默认小于号
        /// </summary>
        public string PageUpText
        {
            get
            {
                return _pageUpText;
            }
            set
            {
                _pageUpText = value;
                RaisePropertyChanged("PageUpText");
            }
        }
        private string _pageDownText = @">>";
        /// <summary>
        /// 后翻页文字内容,默认大于号
        /// </summary>
        public string PageDownText
        {
            get
            {
                return _pageDownText;
            }
            set
            {
                _pageDownText = value;
                RaisePropertyChanged("PageDownText");
            }
        }
        private string _pageNowStringFormat = "{0}/{1}";
        /// <summary>
        /// 当前页文字内容,默认{0}/{1},当前页/总页数
        /// </summary>
        public string PageNowStringFormat
        {
            get
            {
                return _pageNowStringFormat;
            }
            set
            {
                _pageNowStringFormat = value;
                RaisePropertyChanged("PageNowStringFormat");
                RefreshText();
            }
        }

        private string _pageNowText;
        public string PageNowText
        {
            get
            {
                return _pageNowText;
            }
            set
            {
                _pageNowText = value;
                RaisePropertyChanged("PageNowText");
            }
        }
        private SolidColorBrush _textColor = Brushes.Black;
        /// <summary>
        /// 文字颜色,默认黑色
        /// </summary>
        public SolidColorBrush TextColor
        {
            get
            {
                return _textColor;
            }
            set
            {
                _textColor = value;
                RaisePropertyChanged("TextColor");
            }
        }
        private int _totalPage;
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage
        {
            get
            {
                return _totalPage;
            }
            set
            {
                _totalPage = value;
                RaisePropertyChanged("TotalPage");
            }
        }
        private int _pageNow = 1;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNow
        {
            get
            {
                return _pageNow;
            }
            set
            {
                _pageNow = value;
                RaisePropertyChanged("PageNow");
                RefreshEnable();
                RefreshText();
            }
        }
        private int _totalRecords;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecords
        {
            get
            {
                return _totalRecords;
            }
            set
            {
                _totalRecords = value;
                RaisePropertyChanged("TotalRecords");
                CaculatePage();
            }
        }

        public static readonly DependencyProperty TotalRecordsProperty =
            DependencyProperty.Register("TotalRecords", typeof(int), typeof(SimplePageControl),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender, TotalRecordsChanged));

        private static void TotalRecordsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            {
                var ss = sender as SimplePageControl;
                if (ss != null)
                {
                    ss.TotalRecords = int.Parse(e.NewValue.ToString());
                }
            }
            //Application.GetResourceStream(new Uri("pack://application:,,," + (int)e.NewValue));
        }

        private int _recordsPerPage = 10;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int RecordsPerPage
        {
            get
            {
                return _recordsPerPage;
            }
            set
            {
                _recordsPerPage = value;
                RaisePropertyChanged("RecordsPerPage");
                CaculatePage();
            }
        }

        public static readonly DependencyProperty RecordsPerPageProperty =
            DependencyProperty.Register("RecordsPerPage", typeof(int), typeof(SimplePageControl),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender, RecordsPerPageChanged));

        private static void RecordsPerPageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            {
                var ss = sender as SimplePageControl;
                if (ss != null)
                {
                    ss.RecordsPerPage = int.Parse(e.NewValue.ToString());
                }
            }
        }

        private bool _pageUpEnable = false;
        public bool PageUpEnable
        {
            get
            {
                return _pageUpEnable;
            }
            set
            {
                _pageUpEnable = value;
                RaisePropertyChanged("PageUpEnable");
            }
        }
        private bool _pageDownEnable = false;
        public bool PageDownEnable
        {
            get
            {
                return _pageDownEnable;
            }
            set
            {
                _pageDownEnable = value;
                RaisePropertyChanged("PageDownEnable");
            }
        }

        public SimplePageControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        /// <summary>
        /// 分页控件,起始页为1
        /// </summary>
        /// <param name="total_page">总页数</param>
        /// <param name="records_per_page">每页记录数,默认10</param>
        /// <param name="page_now">当前页,默认1</param>
        public SimplePageControl(int total_records, int records_per_page = 10, int page_now = 1)
            : this()
        {
            _totalRecords = total_records;
            _recordsPerPage = records_per_page;
            _pageNow = page_now;
            CaculatePage();
        }

        /// <summary>
        /// 初始化数据,起始页为1
        /// </summary>
        /// <param name="total_page">总页数</param>
        /// <param name="records_per_page">每页记录数,默认10</param>
        /// <param name="page_now">当前页,默认1</param>
        public void InitData(int total_records, int records_per_page = 10, int page_now = 1)
        {
            _totalRecords = total_records;
            _recordsPerPage = records_per_page;
            _pageNow = page_now;
            CaculatePage();
        }

        private void CaculatePage()
        {
            _totalPage = (int)Math.Ceiling(_totalRecords / (float)_recordsPerPage);
            if (_pageNow > _totalPage)
                _pageNow = _totalPage;
            if (_pageNow < 1)
                _pageNow = 1;
            TotalPage = _totalPage;
            PageNow = _pageNow;
            RefreshText();
        }

        private void RefreshText()
        {
            PageNowText = string.Format(PageNowStringFormat, PageNow, TotalPage);
        }

        private void RefreshEnable()
        {
            PageUpEnable = PageNow > 1;
            PageDownEnable = PageNow < TotalPage;
        }

        private void PageUp_Click(object sender, RoutedEventArgs e)
        {
            if (PageNow > 1)
            {
                PageNow--;
                OnPageChanged();
            }
        }

        private void PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (PageNow < TotalPage)
            {
                PageNow++;
                OnPageChanged();
            }
        }

        private void OnPageChanged()
        {
            if (evtPageChanged != null)
            {
                int start = (PageNow - 1) * RecordsPerPage;
                int end = start + RecordsPerPage - 1;
                if (end > TotalRecords - 1)
                    end = TotalRecords - 1;
                evtPageChanged(this, new PageChangedEventArgs(TotalRecords, RecordsPerPage, TotalPage, PageNow, new IndexRange(start, end)));
            }
        }

        #region PropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

    /// <summary>
    /// 数据范围
    /// </summary>
    public class IndexRange
    {
        public int Start
        {
            get;
            set;
        }
        public int End
        {
            get;
            set;
        }
        public IndexRange(int start, int end)
        {
            Start = start;
            End = end;
        }
    }
    /// <summary>
    /// 分页变更事件
    /// </summary>
    public class PageChangedEventArgs : EventArgs
    {
        private int _totalRecords;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecords
        {
            get
            {
                return _totalRecords;
            }
        }
        private int _recordsPerPage;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int RecordsPerPage
        {
            get
            {
                return _recordsPerPage;
            }
        }
        private int _totalPage;
        /// <summary>
        /// 总分页数
        /// </summary>
        public int TotalPage
        {
            get
            {
                return _totalPage;
            }
        }
        private int _pageNow;
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageNow
        {
            get
            {
                return _pageNow;
            }
        }
        private IndexRange _recordsNow;
        /// <summary>
        /// 当前记录范围,从0开始计数
        /// </summary>
        public IndexRange RecordsNow
        {
            get
            {
                return _recordsNow;
            }
        }
        public PageChangedEventArgs(int totalrecords, int recordsperpage, int totalpage, int pagenow, IndexRange recordsnow)
        {
            _totalRecords = totalrecords;
            _recordsPerPage = recordsperpage;
            _totalPage = totalpage;
            _pageNow = pagenow;
            _recordsNow = recordsnow;
        }
        public override string ToString()
        {
            return string.Format("Total:{0}\nPerPage:{1}\nTotalPage:{2}\nPageNow:{3}\nRecordsNow:From{4} To{5}", TotalRecords, RecordsPerPage, TotalPage, PageNow, RecordsNow.Start, RecordsNow.End);
        }
    }
}
